package com.example.demo.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.DemoBootApplication;
import com.example.demo.dao.ResourceDao;
import com.example.demo.model.SysResourceModel;
import com.example.demo.vo.ResourceTree;

@RestController
@RequestMapping("/navs")
public class ResourceController {

	private static final Logger logger = LoggerFactory.getLogger(DemoBootApplication.class);
 
	@Autowired
	ResourceDao resDao;

	@RequestMapping("/list")
	public List<ResourceTree> getList() {
		List<SysResourceModel> resourceList = resDao.findAll();
		logger.info("菜单返回结果:"+queryTreeByPid("0", resourceList));
		return queryTreeByPid("0", resourceList);
	}
	
	private List<ResourceTree> queryTreeByPid(String pid, List<SysResourceModel> list) {
		List<ResourceTree> treeList = new ArrayList<>();
		if (!CollectionUtils.isEmpty(list)) {
			for (SysResourceModel po : list) {
				if (po.getPid().equals(pid)) {
					ResourceTree tree = new ResourceTree();
					tree.setId(po.getId());
					tree.setTitle(po.getResourceName());
					tree.setHref(po.getUrl());
					tree.setChildren(queryTreeByPid(po.getId(), list));
					treeList.add(tree);
				}
			}
		}
		return treeList;
	}
	
	
}